//
//  CollocateFavoriteView.m
//  SecondPage
//
//  Created by xiong qi on 13-4-19.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "CollocateFavoriteView.h"
#import "FavoriteSelectButton.h"

@implementation CollocateFavoriteView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        imageview = [[ScrollImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 493)];
        [self addSubview:imageview];
        [imageview release];
        imageview.parentDelegate = self;
        
        scrollview  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, frame.size.height-75, frame.size.width, 75)];
        [self addSubview:scrollview];
        [scrollview release];
        scrollview.delegate = self;
        
        lastImage = [[UIImageView alloc] initWithFrame:CGRectMake(-24,scrollview.frame.origin.y+19, 24, 37)];
        [self addSubview:lastImage];
        [lastImage release];
        [lastImage setImage:[UIImage imageNamed:@"AllAuto_Config_Flag_Left1"]];
        
        nextImage = [[UIImageView alloc] initWithFrame:CGRectMake(scrollview.frame.size.width,scrollview.frame.origin.y+19, 24, 37)];
        [self addSubview:nextImage];
        [nextImage release];
        [nextImage setImage:[UIImage imageNamed:@"AllAuto_Config_Flag_Right2"]];
    }
    return self;
}

- (NSArray *)GetTableData:(int)index
{
    NSString * selectstring = [NSString stringWithFormat:@"select * from CollocateTable where GearBoxID= %d",parentselectIndex];
    return [[MyCarSQLHelp getInstance] GetTableData:selectstring];
}

-(void)ReadConfig
{
    [super ReadConfig];
    int itemwidth = 0;
    int itemspace = 10;
    if(templateFiles.count>=3)
    {
        itemwidth = (scrollview.frame.size.width-2*itemspace)/3;
    }
    else
    {
        itemwidth = (scrollview.frame.size.width-itemspace)/2;
    }
    
    for (int i=0; i<[templateFiles count]; i++) {
        NSDictionary * dic = [templateFiles objectAtIndex:i];
        FavoriteSelectButton * btn = [[FavoriteSelectButton alloc] initWithFrame:CGRectMake((itemwidth+itemspace)*i, 0, itemwidth, scrollview.frame.size.height)];
        btn.tag = i+100;
        [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setBackGroundNormalImagePath:@"AllAuto_Config_Button_Unselect"];
        [btn setBackGroundHighLightImagePath:@"AllAuto_Config_Button_Select"];
        btn.groupID = 1;
        [scrollview addSubview:btn];
        [btn release];
        [btn RefreshByData:dic];
        
        //默认选择第一个
        if (i==0) {
            [btn sendActionsForControlEvents:UIControlEventTouchUpInside];
            if ([templateFiles count] == 1) {
                
                btn.center = CGPointMake(imageview.center.x, btn.center.y);
            }
        }
        else
        {
            btn.isSelect = NO;
        }
    }
    
    
    
    scrollview.contentSize = CGSizeMake([templateFiles count]*(itemwidth+itemspace)-itemspace, scrollview.frame.size.height);
    if (scrollview.contentSize.width>scrollview.frame.size.width) {
        lastImage.hidden = NO;
        nextImage.hidden = NO;
    }
    else
    {
        lastImage.hidden = YES;
        nextImage.hidden = YES;
    }
    
}

-(void)onClick:(FavoriteSelectButton *)sender
{
    [sender setOnlyHigh];
    NSDictionary * dic = sender.datasource;
    NSString * path = [NSString stringWithFormat:@"%@/%@",self.resourcePath,[dic objectForKey:Favorite_Image_Middle]];
    UIImage * tempimg = [[UIImage alloc]initWithContentsOfFile:path];
    [imageview SetImage:tempimg];
    [tempimg release];
    self.selectIndex = [[dic objectForKey:Favorite_SelectID] intValue];
    self.selectData = dic;
}

-(void)ItemBeTouch:(UIView *)item
{
    if (largeimageview == nil) {
        largeimageview = [[ScrollImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
        [largeimageview setBackgroundColor:[UIColor blackColor]];
    }

    [largeimageview SetImage:imageview.imageview.image];
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window.rootViewController.view addSubview:largeimageview];
    [self ShowLargeImageView];
}

-(void)ShowLargeImageView
{
    [UIView animateWithDuration:0.4
                     animations:^{
                         
                         largeimageview.alpha = 1.0;
                     }
                     completion:^(BOOL finished){
                         
                         
                     }];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollview.contentOffset.x>0) {
        [lastImage setImage:[UIImage imageNamed:@"AllAuto_Config_Flag_Left2"]];
    }
    else
    {
        [lastImage setImage:[UIImage imageNamed:@"AllAuto_Config_Flag_Left1"]];
    }
    
    if (scrollview.contentOffset.x+scrollview.frame.size.width>=scrollview.contentSize.width) {
        [nextImage setImage:[UIImage imageNamed:@"AllAuto_Config_Flag_Right1"]];
    }
    else
    {
        [nextImage setImage:[UIImage imageNamed:@"AllAuto_Config_Flag_Right2"]];
    }
}

-(void)dealloc
{
    SafeRelease(largeimageview);
    [super dealloc];
}
@end
